package it.tukano.collections;

import java.util.ArrayList;

public class History<T> {
    
    private final ArrayList<T> data = new ArrayList<T>();
    private int current = -1;
    
    public void add(T value) {
        if(current == -1) {
            current = 0;
            data.add(value);
        } else {
            ArrayList<T> list = new ArrayList<T>(data.subList(0, current + 1));
            data.clear();
            data.addAll(list);
            data.add(value);
            current = data.size() - 1;
        }
    }

    public T loadPrevious() {
        if(current <= 0) return null;
        
        current--;
        return data.get(current);
    }
    
    public T loadNext() {
        if(current >= data.size() - 1) return null;
        
        current++;
        return data.get(current);
    }
}
